package org.dromara.common.redis.config;


import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.config.properties.RedisProperties;
import org.dromara.common.redis.config.properties.RedissonProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(RedisProperties.class)
public class LettuceConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public RedisClient redisClient() {
        RedisURI redisURI = null;
        if (StringUtils.isEmpty(redisProperties.getPassword())) {
            redisURI = RedisURI.builder()
                .withHost(redisProperties.getHost())
                .withPort(redisProperties.getPort())
                .withDatabase(redisProperties.getDatabase())
                .build();
        } else {
            redisURI = RedisURI.builder()
                .withHost(redisProperties.getHost())
                .withPort(redisProperties.getPort())
                .withDatabase(redisProperties.getDatabase())
                .withPassword(redisProperties.getPassword())
                .build();
        }

        return RedisClient.create(redisURI);
    }



}
